<?php

Raise::load('core.io.file.RaiseFileStream');

/**
 * TestCoreRaiseFileStream
 * Testing unit of 'phpraise/core/RaiseFileStream.php'
 *
 * @author Sam-Mauris Yong / hellclanner at live dot com
 * @license http://www.opensource.org/licenses/bsd-license New BSD License
 * @package Raise.UnitTest.Test
 */
class TestCoreRaiseFileStream extends RaiseTest {

    public static function testStream(){

        self::assertClassExists('RaiseFileStream');
        self::assertClassExtends('RaiseFileStream', 'RaiseStream');

        $mockFile = new RaiseFile('testFile.raise');
        $mockFile->create();

        if($mockFile->exists()){
            $stream = $mockFile->stream();
            self::assertObjectClass($stream, 'RaiseFileStream');

            self::assertTrue($stream->isReadable());
            self::assertTrue($stream->isWriteable());
            self::assertTrue($stream->isSeekable());

            $str = 'hitesting';
            $stream->write($str);
            self::assertStrictEqual($stream->length(), strlen($str));
            self::assertStrictEqual($stream->position(), strlen($str));
            $stream->seek(2);
            self::assertStrictEqual($stream->position(), 2);
            self::assertStrictEqual($stream->read(4), substr($str, 2,4));
            $stream->seek(0);
            self::assertStrictEqual($stream->position(), 0);
            self::assertStrictEqual($stream->read(6), substr($str, 0,6));

            $stream->seek(6);
            $stream->write('eating');
            self::assertStrictEqual($stream->length(), 12);
            $stream->seek(0);
            self::assertStrictEqual($stream->read(12), 'hitesteating');


            $stream->close();
            $mockFile->delete();
        }else{
            self::addError('Unable to create mock file');
        }
    }

}